package com.leyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.entity.Category;
import com.leyou.mapper.CategoryMapper;
import com.leyou.service.CategoryAndBandService;
import com.leyou.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {
    @Autowired
    private CategoryAndBandService categoryAndBandService;

    @Override
    public CategoryDTO getCategoryByCategoryId(long id) {
        Category category = getById(id);
        CategoryDTO categoryDTO = new CategoryDTO(category);
        return categoryDTO;
    }

    @Override
    public List<CategoryDTO> getCategoriesByList(List ids) {
        List<CategoryDTO> categoryDTOS = new ArrayList<>();

        for (Object id : ids) {
            long ida = Long.parseLong((String) id);
            CategoryDTO categoryByCategoryId = getCategoryByCategoryId(ida);
            categoryDTOS.add(categoryByCategoryId);
        }
        return categoryDTOS;

    }

    @Override
    public List<CategoryDTO> getCategoriesByBrandId(Long id) {
        ArrayList<CategoryDTO> categoryDTOS = new ArrayList<>();
        List<Long> categoryIds = categoryAndBandService.findCategoryIdsByBrandId(id);
        for (Long categoryId : categoryIds) {
            CategoryDTO categoryDTO = getCategoryByCategoryId(categoryId);
            categoryDTOS.add(categoryDTO);
        }
        return categoryDTOS;
    }

    @Override
    public List<CategoryDTO> getCategoriesByPid(Long pid) {
        ArrayList<CategoryDTO> categoryDTOS = new ArrayList<>();
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", pid);
        List<Category> categories = baseMapper.selectList(queryWrapper);
        for (Category category : categories) {
            CategoryDTO categoryDTO = new CategoryDTO(category);
            categoryDTOS.add(categoryDTO);
        }

        return categoryDTOS;
    }
}
